#include "facecat/stdafx.h"
#include "LatestDiv.h"
#include "DataCenter.h"
#include "FCDraw.h"

LatestDiv::LatestDiv()
{
    setBorderColor(FCColor_None);
    m_digit = 2;
    m_lV2 = false;
    m_isIndex = false;
}

void LatestDiv::setCode(String value)
{
    m_code = value;
    Security security;
    if (DataCenter::getSecurityService()->getSecurityByCode(m_code,  &security))
    {
        m_isIndex = security.m_type == 0;
    }
    else
    {
        m_isIndex = false;
    }
}

void LatestDiv::drawValue(FCPaint *paint, double value, int digit, FCFont *font, Long color, int x, int y)
{
    if (value == m_latestData.m_upperLimit)
    {
        FCSize pSize = paint->textSize(FCTran::getValueByDigit(value, digit), font, -1);
        FCRect bkRect = {x, y, x + pSize.cx, y + pSize.cy};
        bkRect.right += 1;
        paint->fillRect(FCColor::rgb(255, 82, 82), bkRect);
        FCDraw::drawUnderLineNum2(paint, value, digit, font, FCColor_Back, true, x, y);
    }
    else if (value == m_latestData.m_lowerLimit)
    {
        FCSize pSize = paint->textSize(FCTran::getValueByDigit(value, digit), font, -1);
        FCRect bkRect = {x, y, x + pSize.cx, y + pSize.cy};
        bkRect.right += 1;
        paint->fillRect(FCColor::rgb(82, 255, 82), bkRect);
        FCDraw::drawUnderLineNum2(paint, value, digit, font, FCColor_Back, true, x, y);
    }
    else
    {
        FCDraw::drawUnderLineNum2(paint, value, digit, font, color, true, x, y);
    }
}

void LatestDiv::onPaintForeground(FCPaint *paint, const FCRect &clipRect){
    int width = getWidth();
    int height = getHeight();
    if (width > 0 && height > 0)
    {
        FCFont font(L"Default", 14, false, false, false);
        Long wordColor = USERCOLOR218;
        int top = 32, step = 20;
        if (m_isIndex)
        {
            font.m_fontSize = 14;
            FCDraw::drawText(paint, L"现价", wordColor, &font, 1, top + 20);
            FCDraw::drawText(paint, L"幅度", wordColor, &font, 1, top + 20);
            FCDraw::drawText(paint, L"总量", wordColor, &font, 1, top + 40);
            FCDraw::drawText(paint, L"总额", wordColor, &font, 1, top + 60);
            FCDraw::drawText(paint, L"开盘", wordColor, &font, 110, top);
            FCDraw::drawText(paint, L"最高", wordColor, &font, 110, top + 20);
            FCDraw::drawText(paint, L"最低", wordColor, &font, 110, top + 40);
            FCDraw::drawText(paint, L"振幅", wordColor, &font, 110, top + 60);
            //画股票代码
            Long yellowColor = FCColor::rgb(230, 230, 230);
            if (MyColor::getStyle() == 1)
            {
                yellowColor = FCColor_Text;
            }
            if (m_latestData.m_code.length() > 0)
            {
                Long color = 0;
                double close = m_latestData.m_close, open = m_latestData.m_open, high = m_latestData.m_high, low = m_latestData.m_low, lastClose = m_latestData.m_lastClose;
                if (close == 0)
                {
                    if (m_latestData.m_buyPrice1 > 0)
                    {
                        close = m_latestData.m_buyPrice1;
                    }
                    else if (m_latestData.m_sellPrice1 > 0)
                    {
                        close = m_latestData.m_sellPrice1;
                    }
                }
                if (lastClose == 0)
                {
                    lastClose = close;
                }
                color = FCDraw::getPriceColor(close, lastClose);
                FCDraw::drawUnderLineNum(paint, close, m_digit, &font, color, true, 35, top);
                //升跌
                double sub = 0;
                if (close == 0)
                {
                    sub = m_latestData.m_buyPrice1 - lastClose;
                    double rate = 100 * (m_latestData.m_buyPrice1 - lastClose) / lastClose;
                    if (m_latestData.m_buyPrice1 == 0)
                    {
                        rate = 0;
                        sub = 0;
                    }
                    color = FCDraw::getPriceColor(rate, 0);
                    int pleft = FCDraw::drawUnderLineNum(paint, rate, 2, &font, color, false, 35, top + 20);
                    FCDraw::drawText(paint, L"%", color, &font, pleft + 37, top + 20);
                }
                else
                {
                    color = FCDraw::getPriceColor(close, lastClose);
                    sub = close - m_latestData.m_lastClose;
                    double rate = 100 * (close - lastClose) / lastClose;
                    int pleft = FCDraw::drawUnderLineNum(paint, rate, 2, &font, color, false, 35, top + 20);
                    FCDraw::drawText(paint, L"%", color, &font, pleft + 37, top + 20);
                }
                color = FCDraw::getPriceColor(open, lastClose);
                FCDraw::drawUnderLineNum(paint, open, m_digit, &font, color, true, 145, top);

                double volume = m_latestData.m_volume / 100;
                String unit = L"";
                if (volume > 10000000)
                {
                    volume /= 10000000;
                    unit = L"亿";
                }
                else if (volume > 10000)
                {
                    volume /= 10000;
                    unit = L"万";
                }
                String unit2 = L"";
                double amount = m_latestData.m_amount / 100000000;
                unit2 = L"亿";
                //总手
                int cleft = FCDraw::drawUnderLineNum(paint, volume, unit.length() > 0 ? m_digit : 0, &font, yellowColor, true, 35, top + 40);
                if (unit.length() > 0)
                {
                    FCDraw::drawText(paint, unit, yellowColor, &font, cleft + 37, top + 40);
                }
                color = FCDraw::getPriceColor(high, lastClose);
                FCDraw::drawUnderLineNum(paint, high, m_digit, &font, color, true, 145, top + 20);
                cleft = FCDraw::drawUnderLineNum(paint, amount, unit2.length() > 0 ? m_digit : 0, &font, yellowColor, true, 35, top + 60);
                if (unit2.length() > 0)
                {
                    FCDraw::drawText(paint, unit2, yellowColor, &font, cleft + 37, top + 60);
                }
                color = FCDraw::getPriceColor(low, lastClose);
                FCDraw::drawUnderLineNum(paint, low, m_digit, &font, color, true, 145, top + 40);
                double range = 0;
                if (lastClose != 0)
                {
                    range = 100 * (m_latestData.m_high - lastClose) / lastClose - 100 * (m_latestData.m_low - lastClose) / lastClose;
                }
                cleft = FCDraw::drawUnderLineNum(paint, range, 2, &font, yellowColor, true, 145, top + 60);
                if (range > 0)
                {
                    FCDraw::drawText(paint,L"%", yellowColor, &font, cleft + 147, top + 60);
                }
            }
        }
        else
        {
            //画买卖盘
            FCDraw::drawText(paint, L"卖", wordColor, &font, 1, (m_lV2 ? 87 : 47));
            FCDraw::drawText(paint, L"盘", wordColor, &font, 1, (m_lV2 ? 140 : 100));
            FCDraw::drawText(paint, L"买", wordColor, &font, 1, (m_lV2 ? 267 : 147));
            FCDraw::drawText(paint, L"盘", wordColor, &font, 1, (m_lV2 ? 310 : 200));
            String buySellStr = L"5,4,3,2,1,1,2,3,4,5";
            if (m_lV2)
            {
                step = 16;
//                字符串拼接
                buySellStr = String(L"总卖量,10,9,8,7,6,") + buySellStr + L",6,7,8,9,10,总买量";
                font.m_fontSize = 14;
            }
            ArrayList<String> buySellStrs = FCTran::split(buySellStr, L",");
//            String[] buySellStrs = buySellStr.Split(',');
            int strsSize = buySellStrs.size();
            for (int i = 0; i < strsSize; i++)
            {
//                中括号改成get
                FCDraw::drawText(paint, buySellStrs.get(i), wordColor, &font, 25, top);
                top += step;
            }
            font.m_fontSize = 14;
            top = m_lV2 ? 390 : 232;
            FCDraw::drawText(paint, L"现价", wordColor, &font, 1, top);
            FCDraw::drawText(paint, L"幅度", wordColor, &font, 1, top + 20);
            FCDraw::drawText(paint, L"总量", wordColor, &font, 1, top + 40);
            FCDraw::drawText(paint, L"总额", wordColor, &font, 1, top + 60);
            FCDraw::drawText(paint, L"换手", wordColor, &font, 1, top + 80);
            FCDraw::drawText(paint, L"PE", wordColor, &font, 1, top + 100);
            FCDraw::drawText(paint, L"股本", wordColor, &font, 1, top + 120);
            FCDraw::drawText(paint, L"流通", wordColor, &font, 1, top + 140);
            FCDraw::drawText(paint, L"均价", wordColor, &font, 110, top);
            FCDraw::drawText(paint, L"开盘", wordColor, &font, 110, top + 20);
            FCDraw::drawText(paint, L"最高", wordColor, &font, 110, top + 40);
            FCDraw::drawText(paint, L"最低", wordColor, &font, 110, top + 60);
            FCDraw::drawText(paint, L"振幅", wordColor, &font, 110, top + 80);
            FCDraw::drawText(paint, L"量比", wordColor, &font, 110, top + 100);
            FCDraw::drawText(paint, L"市值", wordColor, &font, 110, top + 120);
            FCDraw::drawText(paint, L"流值", wordColor, &font, 110, top + 140);
            //画股票代码
            Long yellowColor = FCColor::rgb(190, 190, 235);
            if (MyColor::getStyle() == 1)
            {
                yellowColor = FCColor_Text;
            }
            if (m_latestData.m_code.length() > 0)
            {
                double close = m_latestData.m_close, open = m_latestData.m_open, high = m_latestData.m_high, low = m_latestData.m_low, lastClose = m_latestData.m_lastClose;
                if (close == 0)
                {
                    if (m_latestData.m_buyPrice1 > 0)
                    {
                        close = m_latestData.m_buyPrice1;
                    }
                    else if (m_latestData.m_sellPrice1 > 0)
                    {
                        close = m_latestData.m_sellPrice1;
                    }
                }
                if (lastClose == 0)
                {
                    lastClose = close;
                }
                ArrayList<double> plist;
                ArrayList<double> vlist;
                int multi = 100;
                plist.add(m_latestData.m_sellPrice5);
                plist.add(m_latestData.m_sellPrice4);
                plist.add(m_latestData.m_sellPrice3);
                plist.add(m_latestData.m_sellPrice2);
                plist.add(m_latestData.m_sellPrice1);
                vlist.add(m_latestData.m_sellVolume5 / multi);
                vlist.add(m_latestData.m_sellVolume4 / multi);
                vlist.add(m_latestData.m_sellVolume3 / multi);
                vlist.add(m_latestData.m_sellVolume2 / multi);
                vlist.add(m_latestData.m_sellVolume1 / multi);
                plist.add(m_latestData.m_buyPrice1);
                plist.add(m_latestData.m_buyPrice2);
                plist.add(m_latestData.m_buyPrice3);
                plist.add(m_latestData.m_buyPrice4);
                plist.add(m_latestData.m_buyPrice5);
                vlist.add(m_latestData.m_buyVolume1 / multi);
                vlist.add(m_latestData.m_buyVolume2 / multi);
                vlist.add(m_latestData.m_buyVolume3 / multi);
                vlist.add(m_latestData.m_buyVolume4 / multi);
                vlist.add(m_latestData.m_buyVolume5 / multi);
                Long color = 0;
                double mx = 0;
                for (int i = 0; i < vlist.size(); i++)
                {
                    if (i == 0)
                    {
                        mx = vlist.get(i);
                    }
                    else
                    {
                        if (mx < vlist.get(i))
                        {
                            mx = vlist.get(i);
                        }
                    }
                }
                font.m_fontSize = m_lV2 ? 14 : 14;
                if (mx > 0)
                {
                    //绘制买卖盘
                    int pLength = plist.size();
                    top = 32;
                    if (m_lV2)
                    {
                        top += step;
                    }
                    for (int i = 0; i < pLength; i++)
                    {
                        color = FCDraw::getPriceColor(plist.get(i), lastClose);
                        drawValue(paint, plist.get(i), m_digit, &font, color, m_lV2 ? 80 : 60, top);
                        if (i < 5)
                        {
                            color = FCDraw::getPriceColor(0.5, 1);
                        }
                        else
                        {
                            color = FCDraw::getPriceColor(1, 0.5);
                        }
                        FCDraw::drawUnderLineNum2(paint, vlist.get(i), 0, &font, color, true, m_lV2 ? 130 : 110, top);
                        top += step;
                    }
                    if (m_lV2)
                    {
                        top += step;
                    }
                }
                vlist.clear();
                plist.clear();
                top = m_lV2 ? 390 : 232;
                //成交
                color = FCDraw::getPriceColor(close, lastClose);
                drawValue(paint, close, m_digit, &font, color, 35, top);
                //均价
                if (m_latestData.m_volume > 0)
                {
                    double avgPrice = m_latestData.m_amount / m_latestData.m_volume;
                    color = FCDraw::getPriceColor(avgPrice, lastClose);
                    drawValue(paint, avgPrice, m_digit, &font, color, 145, top);
                }
                else
                {
                    FCDraw::drawText(paint, L"-", wordColor, &font, 145, top);
                }
                //升跌
                double sub = 0;
                if (close == 0)
                {
                    sub = m_latestData.m_buyPrice1 - lastClose;
                    double rate = 100 * (m_latestData.m_buyPrice1 - lastClose) / lastClose;
                    if (m_latestData.m_buyPrice1 == 0)
                    {
                        rate = 0;
                        sub = 0;
                    }
                    color = FCDraw::getPriceColor(rate, 0);
                    int pleft = FCDraw::drawUnderLineNum(paint, rate, 2, &font, color, false, 35, top + 20);
                    FCDraw::drawText(paint, L"%", color, &font, pleft + 37, top + 20);
                }
                else
                {
                    color = FCDraw::getPriceColor(close, lastClose);
                    sub = close - m_latestData.m_lastClose;
                    double rate = 100 * (close - lastClose) / lastClose;
                    int pleft = FCDraw::drawUnderLineNum(paint, rate, 2, &font, color, false, 35, top + 20);
                    FCDraw::drawText(paint, L"%", color, &font, pleft + 37, top + 20);
                }
                color = FCDraw::getPriceColor(open, lastClose);
                drawValue(paint, open, m_digit, &font, color, 145, top + 20);

                double volume = m_latestData.m_volume / 100;
                String unit = L"";
                if (volume > 10000000)
                {
                    volume /= 10000000;
                    unit = L"亿";
                }
                else if (volume > 10000)
                {
                    volume /= 10000;
                    unit = L"万";
                }
                String unit2 = L"";
                double amount = m_latestData.m_amount / 100000000;
                unit2 = L"亿";
                //总手
                int cleft = FCDraw::drawUnderLineNum(paint, volume, unit.length() > 0 ? m_digit : 0, &font, yellowColor, true, 35, top + 40);
                if (unit.length() > 0)
                {
                    FCDraw::drawText(paint, unit, yellowColor, &font, cleft + 37, top + 40);
                }
                color = FCDraw::getPriceColor(high, lastClose);
                drawValue(paint, high, m_digit, &font, color, 145, top + 40);
                cleft = FCDraw::drawUnderLineNum(paint, amount, unit2.length() > 0 ? m_digit : 0, &font, yellowColor, true, 35, top + 60);
                if (unit2.length() > 0)
                {
                    FCDraw::drawText(paint, unit2, yellowColor, &font, cleft + 37, top + 60);
                }
                color = FCDraw::getPriceColor(low, lastClose);
                drawValue(paint, low, m_digit, &font, color, 145, top + 60);
                double turnoverRate = m_latestData.m_turnoverRate;
                cleft = FCDraw::drawUnderLineNum(paint, turnoverRate, 2, &font, yellowColor, false, 35, top + 80);
                FCDraw::drawText(paint, L"%", yellowColor, &font, cleft + 37, top + 80);
                double range = 0;
                if (lastClose != 0)
                {
                    range = 100 * (m_latestData.m_high - lastClose) / lastClose - 100 * (m_latestData.m_low - lastClose) / lastClose;
                }
                cleft = FCDraw::drawUnderLineNum(paint, range, 2, &font, yellowColor, true, 145, top + 80);
                if (range > 0)
                {
                    FCDraw::drawText(paint, L"%", yellowColor, &font, cleft + 147, top + 80);
                }
                if (m_latestData.m_pe < 0)
                {
                    FCDraw::drawText(paint, L"亏损", yellowColor, &font, 35, top + 100);
                }
                else
                {
                    FCDraw::drawUnderLineNum(paint, m_latestData.m_pe, m_digit, &font, yellowColor, true, 35, top + 100);
                }
                FCDraw::drawUnderLineNum(paint, m_latestData.m_ratio, m_digit, &font, yellowColor, true, 145, top + 100);
                double equity = m_latestData.m_equity;

                if (equity > 100000000)
                {
                    equity /= 100000000;
                    unit = L"亿";
                }
                else if (equity > 10000)
                {
                    equity /= 10000;
                    unit = L"万";
                }
                cleft = FCDraw::drawUnderLineNum(paint, equity, unit.length() > 0 ? m_digit : 0, &font, yellowColor, true, 35, top + 120);
                if (unit.length() > 0)
                {
                    FCDraw::drawText(paint, unit, yellowColor, &font, cleft + 37, top + 120);
                }
                double flowEquity = m_latestData.m_flowEquity;
                if (flowEquity > 100000000)
                {
                    flowEquity /= 100000000;
                    unit = L"亿";
                }
                else if (flowEquity > 10000)
                {
                    flowEquity /= 10000;
                    unit = L"万";
                }
                cleft = FCDraw::drawUnderLineNum(paint, flowEquity, unit.length() > 0 ? m_digit : 0, &font, yellowColor, true, 35, top + 140);
                if (unit.length() > 0)
                {
                    FCDraw::drawText(paint, unit, yellowColor, &font, cleft + 37, top + 140);
                }
                cleft = FCDraw::drawUnderLineNum(paint, m_latestData.m_marketValue / 100000000, 2, &font, yellowColor, true, 145, top + 120);
                String unit3 = L"亿";
                if (unit3.length() > 0)
                {
                    FCDraw::drawText(paint, unit3, yellowColor, &font, cleft + 147, top + 120);
                }
                cleft = FCDraw::drawUnderLineNum(paint, m_latestData.m_flow / 100000000, 2, &font, yellowColor, true, 145, top + 140);
                if (unit3.length() > 0)
                {
                    FCDraw::drawText(paint, unit3, yellowColor, &font, cleft + 147, top + 140);
                }
            }
            paint->drawLine(FCColor_Border, 1, 0, 0, 0, 0, height);
            paint->drawLine(FCColor_Border, 1, 0, 0, 30, width, 30);
            paint->drawLine(FCColor_Border, 1, 0, 24, 30, 24, top - 2);
            paint->drawLine(FCColor_Border, 1, 0, 0, 131, width, 131);
            paint->drawLine(FCColor_Border, 1, 0, 0, top - 2, width, top - 2);
            paint->drawLine(FCColor_Border, 1, 0, width, 0, width, height);
        }
        font.m_bold = false;
        font.m_fontSize = 14;
        //股票代码
        if (m_code.length() > 0)
        {
            String showCode = m_code.substr(0, m_code.find(L"."));
            FCDraw::drawText(paint, showCode, USERCOLOR218, &font, 5, 4);
        }
        //股票名称
        if (m_sName.length() > 0)
        {
            FCDraw::drawText(paint, m_sName, USERCOLOR217, &font, 55, 4);
        }
    }
}

void LatestDiv::bindData(SecurityLatestData latestData, bool refresh)
{
    m_latestData = latestData;
    setCode(m_latestData.m_code);
    Security security;
    if (DataCenter::getSecurityService()->getSecurityByCode(m_latestData.m_code, &security))
    {
        m_sName = security.m_name;
    }
    else
    {
        m_sName = L"";
    }
    if (refresh)
    {
        invalidate();
    }
}


